package co.yixiang.yshop.module.crm.dal.mysql.basestorage;

import co.yixiang.yshop.framework.common.pojo.PageResult;
import co.yixiang.yshop.framework.mybatis.core.mapper.BaseMapperX;
import co.yixiang.yshop.framework.mybatis.core.query.LambdaQueryWrapperX;
import co.yixiang.yshop.module.crm.controller.admin.basestorage.vo.BaseStoragePageReqVO;
import co.yixiang.yshop.module.crm.dal.dataobject.basestorage.BaseStorageDO;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

/**
 * 仓库 Mapper
 *
 * @author yshop
 */
@Mapper
public interface BaseStorageMapper extends BaseMapperX<BaseStorageDO> {

    default PageResult<BaseStorageDO> selectPage(BaseStoragePageReqVO reqVO, Long currentUserId,boolean isAdministrator) {
        LambdaQueryWrapperX<BaseStorageDO> queryWrapper = new LambdaQueryWrapperX<>();
        queryWrapper
                .likeIfPresent(BaseStorageDO::getStorageCode, reqVO.getStorageCode())
                .likeIfPresent(BaseStorageDO::getStorageName, reqVO.getStorageName())
                .likeIfPresent(BaseStorageDO::getStorageAddress, reqVO.getStorageAddress())
                .eqIfPresent(BaseStorageDO::getStorageType, reqVO.getStorageType())
                .eqIfPresent(BaseStorageDO::getStorageStar, reqVO.getStorageStar())
                .eqIfPresent(BaseStorageDO::getHousekeeperStar, reqVO.getHousekeeperStar())
                .likeIfPresent(BaseStorageDO::getServiceHotline, reqVO.getServiceHotline())
                .eqIfPresent(BaseStorageDO::getParentId, reqVO.getParentId())
                .eqIfPresent(BaseStorageDO::getIsPlateManager, reqVO.getIsPlateManager())
                .eqIfPresent(BaseStorageDO::getIsArea, reqVO.getIsArea())
                .eqIfPresent(BaseStorageDO::getIsShelves, reqVO.getIsShelves())
                .eqIfPresent(BaseStorageDO::getProvinceId, reqVO.getProvinceId())
                .eqIfPresent(BaseStorageDO::getCityId, reqVO.getCityId())
                .eqIfPresent(BaseStorageDO::getRegionId, reqVO.getRegionId())
                .likeIfPresent(BaseStorageDO::getContactName, reqVO.getContactName())
                .likeIfPresent(BaseStorageDO::getContactMobileNo, reqVO.getContactMobileNo())
                .eqIfPresent(BaseStorageDO::getEnable, reqVO.getEnable())
                .orderByDesc(BaseStorageDO::getStorageId);

        // 如果当前用户不是管理员，则添加权限过滤条件

        if (!isAdministrator) {
            var sql=" SELECT 1 FROM sys_role_auth_data WHERE user_id = " + currentUserId + " AND node_id = storage_id and deleted=0";
            queryWrapper.exists(sql);
        }

        return selectPage(reqVO,queryWrapper);


    }

    default List<BaseStorageDO> selectList(BaseStoragePageReqVO reqVO) {
        return selectList(new LambdaQueryWrapperX<BaseStorageDO>()
                .likeIfPresent(BaseStorageDO::getStorageCode, reqVO.getStorageCode())
                .likeIfPresent(BaseStorageDO::getStorageName, reqVO.getStorageName())
                .likeIfPresent(BaseStorageDO::getStorageAddress, reqVO.getStorageAddress())
                .eqIfPresent(BaseStorageDO::getStorageType, reqVO.getStorageType())
                .eqIfPresent(BaseStorageDO::getStorageStar, reqVO.getStorageStar())
                .eqIfPresent(BaseStorageDO::getHousekeeperStar, reqVO.getHousekeeperStar())
                .likeIfPresent(BaseStorageDO::getServiceHotline, reqVO.getServiceHotline())
                .eqIfPresent(BaseStorageDO::getParentId, reqVO.getParentId())
                .eqIfPresent(BaseStorageDO::getIsPlateManager, reqVO.getIsPlateManager())
                .eqIfPresent(BaseStorageDO::getIsArea, reqVO.getIsArea())
                .eqIfPresent(BaseStorageDO::getIsShelves, reqVO.getIsShelves())
                .eqIfPresent(BaseStorageDO::getProvinceId, reqVO.getProvinceId())
                .eqIfPresent(BaseStorageDO::getCityId, reqVO.getCityId())
                .eqIfPresent(BaseStorageDO::getRegionId, reqVO.getRegionId())
                .likeIfPresent(BaseStorageDO::getContactName, reqVO.getContactName())
                .likeIfPresent(BaseStorageDO::getContactMobileNo, reqVO.getContactMobileNo())
                .eqIfPresent(BaseStorageDO::getEnable, reqVO.getEnable())
                .orderByDesc(BaseStorageDO::getStorageId));
    }

} 